package com.hellobike.haxing.modules.statics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.statics.dto.OrderDetailStatics;
import com.hellobike.haxing.modules.statics.param.OrderDetailParam;
import com.hellobike.haxing.modules.statics.service.IOrderDetailStaticsService;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * @Description: t_wms_asn_detail
 * @Author: haxing-boot
 * @Date: 2020-01-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/statics/orderDetailStatics")
@Slf4j
public class OrderDetailStaticsController {

    @Autowired
    private IOrderDetailStaticsService orderDetailStaticsService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订单明细统计-导出")
    @GetMapping(value = "/export")
    public ModelAndView queryPageList(@RequestParam(name = "mobile", required = false) String mobile,
                                      @RequestParam(name = "start", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                      @RequestParam(name = "end", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                      @RequestParam(name = "supplierId", required = false) Long supplierId,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10000000") Integer pageSize,
                                      HttpServletRequest req) {
        OrderDetailParam orderDetailParam = new OrderDetailParam(mobile, start, end, supplierId);
        if (orderDetailParam == null) {
            orderDetailParam = new OrderDetailParam();
        }
        LocalDate now = LocalDate.now();
        if (orderDetailParam.getStart() == null) {
            orderDetailParam.setStart(now.minus(1, ChronoUnit.DAYS));
        }
        if (orderDetailParam.getEnd() == null) {
            orderDetailParam.setEnd(now);
        } else {
            orderDetailParam.setEnd(orderDetailParam.getEnd().minus(-1, ChronoUnit.DAYS));
        }
        IPage<OrderDetailStatics> pageList = orderDetailStaticsService.select(orderDetailParam, pageNo, pageSize);
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "订单明细");
        mv.addObject(NormalExcelConstants.CLASS, OrderDetailStatics.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("订单明细", null, "订单明细"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
        return mv;
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订单明细统计-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> list(@RequestParam(name = "mobile", required = false) String mobile,
                          @RequestParam(name = "start", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                          @RequestParam(name = "end", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                          @RequestParam(name = "supplierId", required = false) Long supplierId,
                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                          HttpServletRequest req) {
        OrderDetailParam orderDetailParam = new OrderDetailParam(mobile, start, end, supplierId);
        if (orderDetailParam == null) {
            orderDetailParam = new OrderDetailParam();
        }
        LocalDate now = LocalDate.now();
        if (orderDetailParam.getStart() == null) {
            orderDetailParam.setStart(now.minus(1, ChronoUnit.DAYS));
        }
        if (orderDetailParam.getEnd() == null) {
            orderDetailParam.setEnd(now.plusDays(1));
        } else {
            orderDetailParam.setEnd(orderDetailParam.getEnd().minus(-1, ChronoUnit.DAYS));
        }
        IPage<OrderDetailStatics> pageList = orderDetailStaticsService.select(orderDetailParam, pageNo, pageSize);
        return Result.ok(pageList);
    }
}
